package com.rivulet.doj_backend.tools;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class CommandUtil {
    public static String OSType = "win";
    public static String[] chooseJudgeCommand(String workPath, String languageType) {
        if (languageType.equals("java")) {
            return new String[]{"cmd", "/c", "java Main.java"};
        } else if (languageType.equals("cpp")) {
            return creatCppRunFile(workPath);
        } else if (languageType.equals("py")) {
            return new String[]{"cmd", "/c", "Python Main.py"};
        } else {
            return null;
        }
    }

    private static String[] creatCppRunFile(String workPath) {
        String [] runCppCommand = null;
        try {
            File file = new File(workPath + File.separator + "cpp.bat");
            if (!file.exists()) {
                file.createNewFile();
            }
            String osName = System.getProperty("os.name").toLowerCase();
            String cppBat;
            if (osName.contains(OSType)) {
                cppBat = "@echo off\n" +
                        "g++ -o main Main.cpp\n" +
                        "Main.exe";
                runCppCommand = new String[]{"cmd", "/c", "cpp.bat"};
            } else {
                //不处理mac，默认当linux系统
                cppBat = "#!/bin/bash  \n" +
                        "g++ main.cpp -o MyApp  \n" +
                        "./MyApp";
                runCppCommand = new String[]{"cmd", "/c", "cpp.bat"};
            }
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
            writer.write(cppBat);
            writer.close();
            return runCppCommand;
        } catch (IOException e) {
        } finally {
            return runCppCommand;
        }
    }
}
